package org.jeecg.modules.demo.com.kunpeng.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.procedure.spi.ParameterRegistrationImplementor;
import org.jeecg.modules.demo.com.kunpeng.entity.*;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.OaOtherContractUpdateDTO;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.OaPurchaseContractUpdateDTO;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.OaSaleContractUpdateDTO;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceCode;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceException;
import org.jeecg.modules.demo.com.kunpeng.mapper.*;
import org.jeecg.modules.demo.com.kunpeng.service.IOaContractService;
import org.jeecg.modules.demo.com.kunpeng.service.IOaCustomerService;
import org.jeecg.modules.demo.com.kunpeng.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional
public class OaContractServiceImpl extends ServiceImpl<OaContractMapper, OaContract> implements IOaContractService {

    private static Integer OA_CONTRACT_ID;

    private static Integer CONTRACT_SUFFIX;

    @Autowired
    private OaContractMapper oaContractMapper;

    @Autowired
    private SaleContractCollectionPlanMapper saleContractCollectionPlanMapper;

    @Autowired
    private SaleContractDetailMapper saleContractDetailMapper;

    @Autowired
    private PurchaseContractCollectionPlanMapper purchaseContractCollectionPlanMapper;

    @Autowired
    private PurchaseContractDetailMapper purchaseContractDetailMapper;

    @Autowired
    private OtherContractCollectionPlanMapper otherContractCollectionPlanMapper;

    @Autowired
    private OtherContractDetailMapper otherContractDetailMapper;

    @Autowired
    private OaProjectMapper oaProjectMapper;
    @Autowired
    private IOaCustomerService oaCustomerService;

    /**
     * 插入OA销售合同数据
     * @param oaSaleContractPage
     */
    @Override
    public void insertOaSaleContract(OaSaleContractPage oaSaleContractPage) {

        //判断是否存在相同合同编号和合同名称的数据
        List<OaContract> oaContractsListByCode = oaContractMapper.oaContractListByCode(oaSaleContractPage.getCode());
        List<OaContract> oaContractsListByName = oaContractMapper.oaContractListByName(oaSaleContractPage.getName());
        if(oaContractsListByCode.size()>0 || oaContractsListByName.size()>0){
            String message="合同编号或合同名称重复，请重新填写！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        //提取销售合同基础数据
        OaContract oaContract = collectOaSaleContractDate(oaSaleContractPage);
        List<OaContract> oaContracts = oaContractMapper.oaContractList();

        //合同id
        if(oaContracts.size()>0){
            OaContract oContract = oaContracts.get(oaContracts.size() - 1);
            OA_CONTRACT_ID=oContract.getId();
            log.info("获取到的id值："+OA_CONTRACT_ID);
            oaContract.setId(++OA_CONTRACT_ID);
        }else {
            OA_CONTRACT_ID=1;
        }
        /*OA_CONTRACT_ID=oaContractMapper.selectOaContractMaxId()+1;*/

        //根据客户id获取客户名称
        OaCustomer oaCustomer = oaCustomerService.selectOaCustomerById(oaContract.getCustomerId());
        System.out.println("customerId:"+oaContract.getCustomerId());
        System.out.println("oaCustomer:"+oaCustomer.toString());
        oaContract.setCustomer(oaCustomer.getName());

        log.info("提取出的销售合同数据："+oaContract.toString());
        oaContractMapper.insert(oaContract);

        //提取销售合同收款计划数据
        if(oaSaleContractPage.getOaSaleContractCollectionPlanPageList().size()>0){
            //如果存在数据，遍历获取
            for (OaSaleContractCollectionPlanPage oaSaleContractCollectionPlanPage:oaSaleContractPage.getOaSaleContractCollectionPlanPageList()) {
                SaleContractCollectionPlan saleContractCollectionPlan = collectOaSaleContractCollectionPlanDate(oaSaleContractCollectionPlanPage);
                saleContractCollectionPlan.setScid(OA_CONTRACT_ID.toString());
                //TODO  插入销售合同收款计划
                saleContractCollectionPlanMapper.insert(saleContractCollectionPlan);
            }
        }

        //提取销售合同详情数据
        if(oaSaleContractPage.getOaSaleContractDetailPageList().size()>0){
            //如果存在数据，遍历获取
            for(OaSaleContractDetailPage oaSaleContractDetailPage:oaSaleContractPage.getOaSaleContractDetailPageList()){
                OaProject oaProject = oaProjectMapper.selectOaProjectByName(oaSaleContractDetailPage.getProject_name());
                //判断该项目是否存在关联合同
                if(null!=oaProject){
                    if(oaProject.getContractId()!=0 && null!=oaProject.getContractId()){
                        String message="项目【"+oaProject.getName()+"】已存在关联合同，请重新选择！";
                        throw new ServiceException(ServiceCode.ERR_INSERT,message);
                    }else if(oaProject.getContractId()==0){
                        oaProjectMapper.updateOaProjectContractIdById(oaProject.getId(),oaContract.getId());
                    }
                }

                SaleContractDetail saleContractDetail = collectOaSaleContractDetailDate(oaSaleContractDetailPage);
                saleContractDetail.setScid(OA_CONTRACT_ID.toString());
                //根据项目名称获取项目详情
                if(null!=oaProject){
                    saleContractDetail.setPjid(oaProject.getId().toString());
                }

                //TODO  插入销售合同详情
                saleContractDetailMapper.insert(saleContractDetail);
            }
        }

    }

    /**
     * 插入采购合同数据
     * @param oaPurchaseContractPage
     */
    @Override
    public void insertOaPurchaseContract(OaPurchaseContractPage oaPurchaseContractPage) {

        //判断是否存在相同合同编号和合同名称的数据
        List<OaContract> oaContractsListByCode = oaContractMapper.oaContractListByCode(oaPurchaseContractPage.getCode());
        List<OaContract> oaContractsListByName = oaContractMapper.oaContractListByName(oaPurchaseContractPage.getName());
        if(oaContractsListByCode.size()>0 || oaContractsListByName.size()>0){
            String message="合同编号或合同名称重复，请重新填写！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        //提取采购合同基础数据
        OaContract oaContract = collectOaPurchaseContractDate(oaPurchaseContractPage);
        List<OaContract> oaContracts = oaContractMapper.oaContractList();

        //合同id
        if(oaContracts.size()>0){
            OaContract oContract = oaContracts.get(oaContracts.size() - 1);
            OA_CONTRACT_ID=oContract.getId();
            log.info("获取到的id值："+OA_CONTRACT_ID);
            oaContract.setId(++OA_CONTRACT_ID);
        }else {
            OA_CONTRACT_ID=1;
        }

        //根据客户id获取客户名称
        OaCustomer oaCustomer = oaCustomerService.selectOaCustomerById(oaContract.getCustomerId());
        oaContract.setCustomer(oaCustomer.getName());

        log.info("提取出的采购合同数据："+oaContract.toString());
        oaContractMapper.insert(oaContract);

        //提取采购合同收款计划数据
        if(oaPurchaseContractPage.getOaPurchaseContractCollectionPlanPageList().size()>0){
            //如果存在数据，遍历获取
            for (OaPurchaseContractCollectionPlanPage oaPurchaseContractCollectionPlanPage:oaPurchaseContractPage.getOaPurchaseContractCollectionPlanPageList()) {
                SaleContractCollectionPlan saleContractCollectionPlan = collectOaPurchaseContractCollectionPlanDate(oaPurchaseContractCollectionPlanPage);
                saleContractCollectionPlan.setScid(OA_CONTRACT_ID.toString());
                //TODO  插入采购合同收款计划
                saleContractCollectionPlanMapper.insert(saleContractCollectionPlan);
            }
        }

        //提取采购合同详情数据
        if(oaPurchaseContractPage.getOaPurchaseContractDetailPageList().size()>0){
            //如果存在数据，遍历获取
            for(OaPurchaseContractDetailPage oaPurchaseContractDetailPage:oaPurchaseContractPage.getOaPurchaseContractDetailPageList()){
                PurchaseContractDetail purchaseContractDetail = collectOaPurchaseContractDetailDate(oaPurchaseContractDetailPage);
                purchaseContractDetail.setScid(OA_CONTRACT_ID.toString());
                //TODO  插入采购合同详情
                purchaseContractDetailMapper.insert(purchaseContractDetail);
            }
        }
    }


    /**
     * 插入其他合同数据
     * @param oaOtherContractPag
     */
    @Override
    public void insertOaOtherContract(OaOtherContractPage oaOtherContractPag) {

        //判断是否存在相同合同编号和合同名称的数据
        List<OaContract> oaContractsListByCode = oaContractMapper.oaContractListByCode(oaOtherContractPag.getCode());
        List<OaContract> oaContractsListByName = oaContractMapper.oaContractListByName(oaOtherContractPag.getName());

        if(oaContractsListByCode.size()>0 || oaContractsListByName.size()>0){
            String message="合同编号或合同名称重复，请重新填写！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        //提取其他合同基础数据
        OaContract oaContract = collectOaOtherContractDate(oaOtherContractPag);
        List<OaContract> oaContracts = oaContractMapper.oaContractList();

        //合同id
        if(oaContracts.size()>0){
            OaContract oContract = oaContracts.get(oaContracts.size() - 1);
            OA_CONTRACT_ID=oContract.getId();
            log.info("获取到的id值："+OA_CONTRACT_ID);
            oaContract.setId(++OA_CONTRACT_ID);
        }else {
            OA_CONTRACT_ID=1;
        }

        //根据客户id获取客户名称
        OaCustomer oaCustomer = oaCustomerService.selectOaCustomerById(oaContract.getCustomerId());
        oaContract.setCustomer(oaCustomer.getName());

        log.info("提取出的其他合同数据："+oaContract.toString());
        oaContractMapper.insert(oaContract);

        //提取其他合同收款计划数据
        if(oaOtherContractPag.getOaOtherContractCollectionPlanPageList().size()>0){
            //如果存在数据，遍历获取
            for (OaOtherContractCollectionPlanPage oaOtherContractCollectionPlanPage:oaOtherContractPag.getOaOtherContractCollectionPlanPageList()) {
                SaleContractCollectionPlan saleContractCollectionPlan = collectOaOtherContractCollectionPlanDate(oaOtherContractCollectionPlanPage);
                saleContractCollectionPlan.setScid(OA_CONTRACT_ID.toString());
                //TODO  插入其他合同收款计划
                saleContractCollectionPlanMapper.insert(saleContractCollectionPlan);
            }
        }

        //提取其他合同详情数据
        if(oaOtherContractPag.getOaOtherContractDetailPageList().size()>0){
            //如果存在数据，遍历获取
            for(OaOtherContractDetailPage oaOtherContractDetailPage:oaOtherContractPag.getOaOtherContractDetailPageList()){
                OtherContractDetail otherContractDetail = collectOaOtherContractDetailDate(oaOtherContractDetailPage);
                otherContractDetail.setScid(OA_CONTRACT_ID.toString());
                //TODO  插入其他合同详情
                otherContractDetailMapper.insert(otherContractDetail);
            }
        }
    }


    /**
     * 编辑销售合同关联数据
     * @param oaSaleContractUpdateDTO
     */
    @Override
    public void updateOaSaleContract(OaSaleContractUpdateDTO oaSaleContractUpdateDTO) {

        //判断编辑的合同编号是否已存在
        List<OaContract> oaContractsListByCode = oaContractMapper.oaContractListByCode(oaSaleContractUpdateDTO.getCode());
        if(oaContractsListByCode.size()==1 && oaSaleContractUpdateDTO.getId()!=oaContractsListByCode.get(0).getId()){
            String message="该合同编号已存在，请重新编辑！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        //判断编辑的合同名称是否已存在
        List<OaContract> oaContractsListByName = oaContractMapper.oaContractListByName(oaSaleContractUpdateDTO.getName());
        if(oaContractsListByName.size()==1 && !oaSaleContractUpdateDTO.getName().equals(oaContractsListByName.get(0).getName())){
            String message="该合同名称已存在，请重新编辑！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }

        //删除详情和计划关联数据
        System.out.println("编辑销售合同id："+oaSaleContractUpdateDTO.getId().toString());
        saleContractDetailMapper.deleteByMainId(oaSaleContractUpdateDTO.getId().toString());
        saleContractCollectionPlanMapper.deleteByMainId(oaSaleContractUpdateDTO.getId().toString());

        OaSaleContractPage oaSaleContractPage=new OaSaleContractPage();
        BeanUtils.copyProperties(oaSaleContractUpdateDTO,oaSaleContractPage);
        oaSaleContractPage.setCost(new BigDecimal(oaSaleContractUpdateDTO.getCost()));
        oaSaleContractPage.setTax(new BigDecimal(oaSaleContractUpdateDTO.getTax()));
        oaSaleContractPage.setId(oaSaleContractUpdateDTO.getId());
        System.out.println("转换为销售合同数据："+oaSaleContractPage.toString());

        //提取销售合同基础数据
        OaContract oaContract = collectOaSaleContractDate(oaSaleContractPage);
        oaContract.setId(oaSaleContractPage.getId());
        System.out.println("提取的销售合同数据："+oaContract.toString());
        int num = oaContractMapper.updateById(oaContract);
        if(num!=1){
            String message="编辑失败，请联系管理员！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }

        //提取销售合同收款计划数据
        if(oaSaleContractPage.getOaSaleContractCollectionPlanPageList().size()>0){
            //如果存在数据，遍历获取
            for (OaSaleContractCollectionPlanPage oaSaleContractCollectionPlanPage:oaSaleContractPage.getOaSaleContractCollectionPlanPageList()) {
                SaleContractCollectionPlan saleContractCollectionPlan = collectOaSaleContractCollectionPlanDate(oaSaleContractCollectionPlanPage);
                saleContractCollectionPlan.setScid(oaContract.getId().toString());
                //TODO  插入销售合同收款计划
                saleContractCollectionPlanMapper.insert(saleContractCollectionPlan);
            }
        }

        //提取销售合同详情数据
        if(oaSaleContractPage.getOaSaleContractDetailPageList().size()>0){
            //如果存在数据，遍历获取
            for(OaSaleContractDetailPage oaSaleContractDetailPage:oaSaleContractPage.getOaSaleContractDetailPageList()){
                OaProject oaProject = oaProjectMapper.selectOaProjectByName(oaSaleContractDetailPage.getProject_name());
                //判断该项目是否存在关联合同
                if(oaProject.getContractId()!=0 && null!=oaProject.getContractId()){
                    String message="项目【"+oaProject.getName()+"】已存在关联合同，请重新选择！";
                    throw new ServiceException(ServiceCode.ERR_INSERT,message);
                }else if(oaProject.getContractId()==0){
                    oaProjectMapper.updateOaProjectContractIdById(oaProject.getId(),oaContract.getId());
                }

                SaleContractDetail saleContractDetail = collectOaSaleContractDetailDate(oaSaleContractDetailPage);
                saleContractDetail.setScid(oaContract.getId().toString());
                //根据项目名称获取项目详情
                if(null!=oaProject){
                    saleContractDetail.setPjid(oaProject.getId().toString());
                }
                //TODO  插入销售合同详情
                saleContractDetailMapper.insert(saleContractDetail);
            }
        }
    }

    /**
     * 编辑采购合同关联数据
     * @param oaPurchaseContractUpdateDTO
     */
    @Override
    public void updateOaPurchaseContract(OaPurchaseContractUpdateDTO oaPurchaseContractUpdateDTO) {

        //判断编辑的合同编号是否已存在
        List<OaContract> oaContractsListByCode = oaContractMapper.oaContractListByCode(oaPurchaseContractUpdateDTO.getCode());
        if(oaContractsListByCode.size()==1 && oaPurchaseContractUpdateDTO.getId()!=oaContractsListByCode.get(0).getId()){
            String message="该合同编号已存在，请重新编辑！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        //判断编辑的合同名称是否已存在
        List<OaContract> oaContractsListByName = oaContractMapper.oaContractListByName(oaPurchaseContractUpdateDTO.getName());
        if(oaContractsListByName.size()==1 && !oaPurchaseContractUpdateDTO.getName().equals(oaContractsListByName.get(0).getName())){
            String message="该合同名称已存在，请重新编辑！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }

        //删除详情和计划关联数据
        purchaseContractDetailMapper.deleteByMainId(oaPurchaseContractUpdateDTO.getId().toString());
        saleContractCollectionPlanMapper.deleteByMainId(oaPurchaseContractUpdateDTO.getId().toString());

        OaPurchaseContractPage oaPurchaseContractPage=new OaPurchaseContractPage();
        BeanUtils.copyProperties(oaPurchaseContractUpdateDTO,oaPurchaseContractPage);
        oaPurchaseContractPage.setCost(new BigDecimal(oaPurchaseContractUpdateDTO.getCost()));
        oaPurchaseContractPage.setTax(new BigDecimal(oaPurchaseContractUpdateDTO.getTax()));
        oaPurchaseContractPage.setId(oaPurchaseContractUpdateDTO.getId());
        System.out.println("转换为采购合同数据："+oaPurchaseContractPage.toString());

        //提取采购合同基础数据
        OaContract oaContract = collectOaPurchaseContractDate(oaPurchaseContractPage);
        oaContract.setId(oaPurchaseContractPage.getId());
        System.out.println("提取的采购合同数据："+oaContract.toString());

        int num = oaContractMapper.updateById(oaContract);
        if(num!=1){
            String message="编辑失败，请联系管理员！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }

        //提取采购合同收款计划数据
        if(oaPurchaseContractPage.getOaPurchaseContractCollectionPlanPageList().size()>0){
            //如果存在数据，遍历获取
            for (OaPurchaseContractCollectionPlanPage oaPurchaseContractCollectionPlanPage:oaPurchaseContractPage.getOaPurchaseContractCollectionPlanPageList()) {
                SaleContractCollectionPlan saleContractCollectionPlan = collectOaPurchaseContractCollectionPlanDate(oaPurchaseContractCollectionPlanPage);
                saleContractCollectionPlan.setScid(oaContract.getId().toString());
                //TODO  插入采购合同收款计划
                saleContractCollectionPlanMapper.insert(saleContractCollectionPlan);
            }
        }

        //提取采购合同详情数据
        if(oaPurchaseContractPage.getOaPurchaseContractDetailPageList().size()>0){
            //如果存在数据，遍历获取
            for(OaPurchaseContractDetailPage oaPurchaseContractDetailPage:oaPurchaseContractPage.getOaPurchaseContractDetailPageList()){
                PurchaseContractDetail purchaseContractDetail = collectOaPurchaseContractDetailDate(oaPurchaseContractDetailPage);
                purchaseContractDetail.setScid(oaContract.getId().toString());
                //TODO  插入采购合同详情
                purchaseContractDetailMapper.insert(purchaseContractDetail);
            }
        }

    }

    /**
     * 编辑其他合同关联数据
     * @param oaOtherContractUpdateDTO
     */
    @Override
    public void updateOaOtherContract(OaOtherContractUpdateDTO oaOtherContractUpdateDTO) {

        //判断编辑的合同编号是否已存在
        List<OaContract> oaContractsListByCode = oaContractMapper.oaContractListByCode(oaOtherContractUpdateDTO.getCode());
        if(oaContractsListByCode.size()==1 && oaOtherContractUpdateDTO.getId()!=oaContractsListByCode.get(0).getId()){
            String message="该合同编号已存在，请重新编辑！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        //判断编辑的合同名称是否已存在
        List<OaContract> oaContractsListByName = oaContractMapper.oaContractListByName(oaOtherContractUpdateDTO.getName());
        if(oaContractsListByName.size()==1 && !oaOtherContractUpdateDTO.getName().equals(oaContractsListByName.get(0).getName())){
            String message="该合同名称已存在，请重新编辑！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }

        //删除详情和计划关联数据
        otherContractDetailMapper.deleteByMainId(oaOtherContractUpdateDTO.getId().toString());
        saleContractCollectionPlanMapper.deleteByMainId(oaOtherContractUpdateDTO.getId().toString());

        OaOtherContractPage oaOtherContractPage=new OaOtherContractPage();
        BeanUtils.copyProperties(oaOtherContractUpdateDTO,oaOtherContractPage);
        oaOtherContractPage.setCost(new BigDecimal(oaOtherContractUpdateDTO.getCost()));
        oaOtherContractPage.setTax(new BigDecimal(oaOtherContractUpdateDTO.getTax()));
        oaOtherContractPage.setId(oaOtherContractUpdateDTO.getId());
        System.out.println("转换为其他合同数据："+oaOtherContractPage.toString());

        //提取其他合同基础数据
        OaContract oaContract = collectOaOtherContractDate(oaOtherContractPage);
        oaContract.setId(oaOtherContractPage.getId());
        System.out.println("提取的其他合同数据："+oaContract.toString());

        int num = oaContractMapper.updateById(oaContract);
        if(num!=1){
            String message="编辑失败，请联系管理员！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }

        //提取其他合同收款计划数据
        if(oaOtherContractPage.getOaOtherContractCollectionPlanPageList().size()>0){
            //如果存在数据，遍历获取
            for (OaOtherContractCollectionPlanPage oaOtherContractCollectionPlanPage:oaOtherContractPage.getOaOtherContractCollectionPlanPageList()) {
                SaleContractCollectionPlan saleContractCollectionPlan = collectOaOtherContractCollectionPlanDate(oaOtherContractCollectionPlanPage);
                saleContractCollectionPlan.setScid(oaContract.getId().toString());
                //TODO  插入其他合同收款计划
                saleContractCollectionPlanMapper.insert(saleContractCollectionPlan);
            }
        }

        //提取其他合同详情数据
        if(oaOtherContractPage.getOaOtherContractDetailPageList().size()>0){
            //如果存在数据，遍历获取
            for(OaOtherContractDetailPage oaOtherContractDetailPage:oaOtherContractPage.getOaOtherContractDetailPageList()){
                OtherContractDetail otherContractDetail = collectOaOtherContractDetailDate(oaOtherContractDetailPage);
                otherContractDetail.setScid(oaContract.getId().toString());
                //TODO  插入其他合同详情
                otherContractDetailMapper.insert(otherContractDetail);
            }
        }

    }

    /**
     * 生成合同编号后缀
     * @param prefix
     * @return
     */
    @Override
    public Integer oaContractListByContractIdPrefix(String prefix) {
        List<OaContract> oaContracts = oaContractMapper.oaContractListByContractIdPrefix(prefix);
        if(oaContracts.size()>0){
            OaContract oaContract = oaContracts.get(oaContracts.size() - 1);
            String oaContractCode = oaContract.getCode();
            String substring = oaContractCode.substring(10, 13);
            CONTRACT_SUFFIX = Integer.parseInt(substring)+1;
        }else {
            CONTRACT_SUFFIX=1;
        }
        return CONTRACT_SUFFIX;
    }


    /**
     * 提取OA销售合同基本数据
     */
    public static OaContract collectOaSaleContractDate(OaSaleContractPage oaSaleContractPage){
        OaContract oaContract=new OaContract();
        oaContract.setPid(0);
        oaContract.setCode(oaSaleContractPage.getCode());
        oaContract.setName(oaSaleContractPage.getName());
        oaContract.setCateId(oaSaleContractPage.getCate_id());
        oaContract.setType(oaSaleContractPage.getType());
        oaContract.setSubjectId(oaSaleContractPage.getSubject_id());
        oaContract.setCustomerId(oaSaleContractPage.getCustomer_id());
        oaContract.setCustomer(oaSaleContractPage.getCustomer());
        oaContract.setCustomerName(oaSaleContractPage.getCustomer_name());
        oaContract.setCustomerMobile(oaSaleContractPage.getCustomer_mobile());
        oaContract.setCustomerAddress(oaSaleContractPage.getCustomer_address());

        //日期格式转换
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //转换合同开始时间
            Date start_time = simpleDateFormat.parse(oaSaleContractPage.getStart_time());
            System.out.println("合同开始时间Date类型："+start_time);
            Long startTime = start_time.getTime();
            startTime/=1000L;
            System.out.println("合同开始时间未转成int类型："+startTime);
            oaContract.setStartTime(startTime.intValue());
            System.out.println("合同开始时间转成int类型："+startTime.intValue());

            //转换合同结束时间
            Date end_time = simpleDateFormat.parse(oaSaleContractPage.getEnd_time());
            Long endTime=end_time.getTime();
            endTime/=1000L;
            oaContract.setEndTime(endTime.intValue());

            //转换签订合同时间
            Date sign_time = simpleDateFormat.parse(oaSaleContractPage.getSign_time());
            Long signTime=sign_time.getTime();
            signTime/=1000L;

            //转换合同创建时间
            Long currentTime = System.currentTimeMillis();
            currentTime/=1000L;
            oaContract.setCreateTime(currentTime.intValue());
            oaContract.setSignTime(signTime.intValue());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        oaContract.setAdminId(oaSaleContractPage.getAdmin_id());
        oaContract.setPreparedUid(oaSaleContractPage.getPrepared_uid());
        oaContract.setSignUid(oaSaleContractPage.getSign_uid());
        oaContract.setKeeperUid(oaSaleContractPage.getKeeper_uid());
        oaContract.setShareIds(oaSaleContractPage.getShare_ids());
        oaContract.setFileIds(oaSaleContractPage.getFile_ids());
        oaContract.setSignDid(oaSaleContractPage.getSign_did());
        oaContract.setCost(oaSaleContractPage.getCost());
        oaContract.setIsTax(oaSaleContractPage.getIs_tax());
        oaContract.setTax(oaSaleContractPage.getTax());
        //合同审核状态
        oaContract.setCheckStatus(0);
        oaContract.setCheckStepSort(0);
        //TODO 合同当前审核人未赋值
        oaContract.setCheckAdminIds("");
        //TODO 合同历史审核人未赋值
        oaContract.setFlowAdminIds("");
        oaContract.setLastAdminId("0");
        //TODO 抄送人未赋值
        oaContract.setCopyUids(oaSaleContractPage.getCopy_uids());
        //审核人信息
        oaContract.setCheckUid(0);
        oaContract.setCheckTime(0);

        //中止人信息
        oaContract.setStopUid(0);
        oaContract.setStopTime(0);

        //作废人信息
        oaContract.setVoidUid(0);
        oaContract.setVoidTime(0);

        //规档信息
        oaContract.setArchiveUid(0);
        oaContract.setArchiveStatus(0);
        oaContract.setArchiveTime(0);

        //备注信息
        oaContract.setRemark(oaSaleContractPage.getRemark());

        //收款信息
        oaContract.setFullNameOfPayee(oaSaleContractPage.getFull_name_of_payee());
        oaContract.setTaxRegistrationAccountNo(oaSaleContractPage.getTax_registration_accountNo());
        oaContract.setCollectionAccountNo(oaSaleContractPage.getCollection_accountNo());
        oaContract.setBankOfDeposit(oaSaleContractPage.getBank_of_deposit());
        return oaContract;
    }

    /**
     * 提取OA销售合同收款计划数据
     */
    public static SaleContractCollectionPlan collectOaSaleContractCollectionPlanDate(OaSaleContractCollectionPlanPage oaSaleContractCollectionPlanPage){
        SaleContractCollectionPlan saleContractCollectionPlan=new SaleContractCollectionPlan();
        //收款金额
        saleContractCollectionPlan.setAmountCollected(oaSaleContractCollectionPlanPage.getAmount_collected());
        //收款比例
        saleContractCollectionPlan.setCollectionProportion(oaSaleContractCollectionPlanPage.getCollection_proportion());
        //收款约定时间
        saleContractCollectionPlan.setAgreedCollectionTime(oaSaleContractCollectionPlanPage.getAgreed_collection_time());
        //批次
        saleContractCollectionPlan.setBatch(oaSaleContractCollectionPlanPage.getBatch());
        return saleContractCollectionPlan;
    }

    /**
     * 提取OA销售合同详情数据
     */
    public static SaleContractDetail collectOaSaleContractDetailDate(OaSaleContractDetailPage oaSaleContractDetailPage){
        SaleContractDetail saleContractDetail=new SaleContractDetail();

        //项目编号
        saleContractDetail.setProjectId(oaSaleContractDetailPage.getProject_id());
        //项目名称
        saleContractDetail.setProjectName(oaSaleContractDetailPage.getProject_name());
        //价税合计
        saleContractDetail.setTotalAmount(oaSaleContractDetailPage.getTotal_amount());
        //含税金额
        saleContractDetail.setTaxAmount(oaSaleContractDetailPage.getTax_amount());
        //税率
        saleContractDetail.setTaxRate(oaSaleContractDetailPage.getTax_rate());
        //折扣率
        saleContractDetail.setDiscountRate(oaSaleContractDetailPage.getDiscount_rate());
        //合同金额
        saleContractDetail.setContractAmount(oaSaleContractDetailPage.getContract_amount());
        //到货日期
        saleContractDetail.setPurchaseDate(oaSaleContractDetailPage.getPurchase_date());
        //备注
        saleContractDetail.setRemark(oaSaleContractDetailPage.getRemark());
        return saleContractDetail;
    }


    /**
     * 提取OA采购合同基本数据
     */
    public static OaContract collectOaPurchaseContractDate(OaPurchaseContractPage oaPurchaseContractPage){
        OaContract oaContract=new OaContract();
        oaContract.setPid(0);
        oaContract.setCode(oaPurchaseContractPage.getCode());
        oaContract.setName(oaPurchaseContractPage.getName());
        oaContract.setCateId(oaPurchaseContractPage.getCate_id());
        oaContract.setType(oaPurchaseContractPage.getType());
        oaContract.setSubjectId(oaPurchaseContractPage.getSubject_id());
        oaContract.setCustomerId(oaPurchaseContractPage.getCustomer_id());
        oaContract.setCustomer(oaPurchaseContractPage.getCustomer());
        oaContract.setCustomerName(oaPurchaseContractPage.getCustomer_name());
        oaContract.setCustomerMobile(oaPurchaseContractPage.getCustomer_mobile());
        oaContract.setCustomerAddress(oaPurchaseContractPage.getCustomer_address());

        //日期格式转换
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //转换合同开始时间
            Date start_time = simpleDateFormat.parse(oaPurchaseContractPage.getStart_time());
            System.out.println("合同开始时间Date类型："+start_time);
            Long startTime = start_time.getTime();
            startTime/=1000L;
            System.out.println("合同开始时间未转成int类型："+startTime);
            oaContract.setStartTime(startTime.intValue());
            System.out.println("合同开始时间转成int类型："+startTime.intValue());

            //转换合同结束时间
            Date end_time = simpleDateFormat.parse(oaPurchaseContractPage.getEnd_time());
            Long endTime=end_time.getTime();
            endTime/=1000L;
            oaContract.setEndTime(endTime.intValue());

            //转换签订日期
            Date sign_time = simpleDateFormat.parse(oaPurchaseContractPage.getSign_time());
            Long signTime=sign_time.getTime();
            signTime/=1000L;
            oaContract.setSignTime(signTime.intValue());

            Long currentTime = System.currentTimeMillis();
            currentTime/=1000L;
            oaContract.setCreateTime(currentTime.intValue());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        oaContract.setAdminId(oaPurchaseContractPage.getAdmin_id());
        oaContract.setPreparedUid(oaPurchaseContractPage.getPrepared_uid());
        oaContract.setSignUid(oaPurchaseContractPage.getSign_uid());
        oaContract.setKeeperUid(oaPurchaseContractPage.getKeeper_uid());
        oaContract.setShareIds(oaPurchaseContractPage.getShare_ids());
        oaContract.setFileIds(oaPurchaseContractPage.getFile_ids());
        oaContract.setSignDid(oaPurchaseContractPage.getSign_did());
        oaContract.setCost(oaPurchaseContractPage.getCost());
        oaContract.setIsTax(oaPurchaseContractPage.getIs_tax());
        oaContract.setTax(oaPurchaseContractPage.getTax());
        //合同审核状态
        oaContract.setCheckStatus(0);
        oaContract.setCheckStepSort(0);
        //TODO 合同当前审核人未赋值
        oaContract.setCheckAdminIds("");
        //TODO 合同历史审核人未赋值
        oaContract.setFlowAdminIds("");
        oaContract.setLastAdminId("0");
        //TODO 抄送人未赋值
        oaContract.setCopyUids(oaPurchaseContractPage.getCopy_uids());
        //审核人信息
        oaContract.setCheckUid(0);
        oaContract.setCheckTime(0);

        //中止人信息
        oaContract.setStopUid(0);
        oaContract.setStopTime(0);

        //作废人信息
        oaContract.setVoidUid(0);
        oaContract.setVoidTime(0);

        //规档信息
        oaContract.setArchiveUid(0);
        oaContract.setArchiveStatus(0);
        oaContract.setArchiveTime(0);

        //备注信息
        oaContract.setRemark(oaPurchaseContractPage.getRemark());

        //收款信息
        oaContract.setFullNameOfPayee(oaPurchaseContractPage.getFull_name_of_payee());
        oaContract.setTaxRegistrationAccountNo(oaPurchaseContractPage.getTax_registration_accountNo());
        oaContract.setCollectionAccountNo(oaPurchaseContractPage.getCollection_accountNo());
        oaContract.setBankOfDeposit(oaPurchaseContractPage.getBank_of_deposit());

        return oaContract;
    }

    /**
     * 提取OA采购合同收款计划数据
     */
    public static SaleContractCollectionPlan collectOaPurchaseContractCollectionPlanDate(OaPurchaseContractCollectionPlanPage oaPurchaseContractCollectionPlanPage){
        SaleContractCollectionPlan saleContractCollectionPlan=new SaleContractCollectionPlan();
        //收款金额
        saleContractCollectionPlan.setAmountCollected(oaPurchaseContractCollectionPlanPage.getAmount_collected());
        //收款比例
        saleContractCollectionPlan.setCollectionProportion(oaPurchaseContractCollectionPlanPage.getCollection_proportion());
        //收款约定时间
        saleContractCollectionPlan.setAgreedCollectionTime(oaPurchaseContractCollectionPlanPage.getAgreed_collection_time());
        //批次
        saleContractCollectionPlan.setBatch(oaPurchaseContractCollectionPlanPage.getBatch());
        return saleContractCollectionPlan;
    }

    /**
     * 提取OA采购合同详情数据
     */
    public static PurchaseContractDetail collectOaPurchaseContractDetailDate(OaPurchaseContractDetailPage oaPurchaseContractDetailPage){
        PurchaseContractDetail purchaseContractDetail=new PurchaseContractDetail();

        //商品编号
        purchaseContractDetail.setCommodityId(oaPurchaseContractDetailPage.getCommodity_id());
        //商品名称
        purchaseContractDetail.setCommodityName(oaPurchaseContractDetailPage.getCommodity_name());
        //商品类别
        purchaseContractDetail.setCommodityType(oaPurchaseContractDetailPage.getCommodity_type());
        //规格型号
        purchaseContractDetail.setSpecifications(oaPurchaseContractDetailPage.getSpecifications());
        //商品数量
        purchaseContractDetail.setCompany(oaPurchaseContractDetailPage.getCompany());
        //含税单价
        purchaseContractDetail.setUnitPrice(oaPurchaseContractDetailPage.getUnit_price());
        //合同数量
        purchaseContractDetail.setContractCount(oaPurchaseContractDetailPage.getContract_count());
        //价税合计
        purchaseContractDetail.setTotalAmount(oaPurchaseContractDetailPage.getTotal_amount());
        //含税金额
        purchaseContractDetail.setTaxAmount(oaPurchaseContractDetailPage.getTax_amount());
        //税率
        purchaseContractDetail.setTaxRate(oaPurchaseContractDetailPage.getTax_rate());
        //折扣率
        purchaseContractDetail.setDiscountRate(oaPurchaseContractDetailPage.getDiscount_rate());
        //合同金额
        purchaseContractDetail.setContractAmount(oaPurchaseContractDetailPage.getContract_amount());
        //到货日期
        purchaseContractDetail.setPurchaseDate(oaPurchaseContractDetailPage.getPurchase_date());
        //备注
        purchaseContractDetail.setRemark(oaPurchaseContractDetailPage.getRemark());
        return purchaseContractDetail;
    }

    /**
     * 提取OA其他合同基本数据
     */
    public static OaContract collectOaOtherContractDate(OaOtherContractPage oaOtherContractPage){
        OaContract oaContract=new OaContract();
        oaContract.setPid(0);
        oaContract.setCode(oaOtherContractPage.getCode());
        oaContract.setName(oaOtherContractPage.getName());
        oaContract.setCateId(oaOtherContractPage.getCate_id());
        oaContract.setType(oaOtherContractPage.getType());
        oaContract.setSubjectId(oaOtherContractPage.getSubject_id());
        oaContract.setCustomerId(oaOtherContractPage.getCustomer_id());
        oaContract.setCustomer(oaOtherContractPage.getCustomer());
        oaContract.setCustomerName(oaOtherContractPage.getCustomer_name());
        oaContract.setCustomerMobile(oaOtherContractPage.getCustomer_mobile());
        oaContract.setCustomerAddress(oaOtherContractPage.getCustomer_address());

        //日期格式转换
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //转换合同开始时间
            Date start_time = simpleDateFormat.parse(oaOtherContractPage.getStart_time());
            System.out.println("合同开始时间Date类型："+start_time);
            Long startTime = start_time.getTime();
            startTime/=1000L;
            System.out.println("合同开始时间未转成int类型："+startTime);
            oaContract.setStartTime(startTime.intValue());
            System.out.println("合同开始时间转成int类型："+startTime.intValue());
            //转换合同结束时间
            Date end_time = simpleDateFormat.parse(oaOtherContractPage.getEnd_time());
            Long endTime=end_time.getTime();
            endTime/=1000L;
            oaContract.setEndTime(endTime.intValue());
            //转换合同签订时间
            Date sign_time = simpleDateFormat.parse(oaOtherContractPage.getSign_time());
            Long signTime=sign_time.getTime();
            signTime/=1000L;
            oaContract.setSignTime(signTime.intValue());
            //转换合同创建时间
            Long currentTime = System.currentTimeMillis();
            currentTime/=1000L;
            oaContract.setCreateTime(currentTime.intValue());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        oaContract.setAdminId(oaOtherContractPage.getAdmin_id());
        oaContract.setPreparedUid(oaOtherContractPage.getPrepared_uid());
        oaContract.setSignUid(oaOtherContractPage.getSign_uid());
        oaContract.setKeeperUid(oaOtherContractPage.getKeeper_uid());
        oaContract.setShareIds(oaOtherContractPage.getShare_ids());
        oaContract.setFileIds(oaOtherContractPage.getFile_ids());
        oaContract.setSignDid(oaOtherContractPage.getSign_did());
        oaContract.setCost(oaOtherContractPage.getCost());
        oaContract.setIsTax(oaOtherContractPage.getIs_tax());
        oaContract.setTax(oaOtherContractPage.getTax());
        //合同审核状态
        oaContract.setCheckStatus(0);
        oaContract.setCheckStepSort(0);
        //TODO 合同当前审核人未赋值
        oaContract.setCheckAdminIds("");
        //TODO 合同历史审核人未赋值
        oaContract.setFlowAdminIds("");
        oaContract.setLastAdminId("0");
        //TODO 抄送人未赋值
        oaContract.setCopyUids(oaOtherContractPage.getCopy_uids());
        //审核人信息
        oaContract.setCheckUid(0);
        oaContract.setCheckTime(0);

        //中止人信息
        oaContract.setStopUid(0);
        oaContract.setStopTime(0);

        //作废人信息
        oaContract.setVoidUid(0);
        oaContract.setVoidTime(0);

        //规档信息
        oaContract.setArchiveUid(0);
        oaContract.setArchiveStatus(0);
        oaContract.setArchiveTime(0);

        //备注信息
        oaContract.setRemark(oaOtherContractPage.getRemark());

        //收款信息
        oaContract.setFullNameOfPayee(oaOtherContractPage.getFull_name_of_payee());
        oaContract.setTaxRegistrationAccountNo(oaOtherContractPage.getTax_registration_accountNo());
        oaContract.setCollectionAccountNo(oaOtherContractPage.getCollection_accountNo());
        oaContract.setBankOfDeposit(oaOtherContractPage.getBank_of_deposit());

        return oaContract;
    }


    /**
     * 提取OA其他合同收款计划数据
     */
    public static SaleContractCollectionPlan collectOaOtherContractCollectionPlanDate(OaOtherContractCollectionPlanPage oaOtherContractCollectionPlanPage){
        SaleContractCollectionPlan saleContractCollectionPlan=new SaleContractCollectionPlan();
        //收款金额
        saleContractCollectionPlan.setAmountCollected(oaOtherContractCollectionPlanPage.getAmount_collected());
        //收款比例
        saleContractCollectionPlan.setCollectionProportion(oaOtherContractCollectionPlanPage.getCollection_proportion());
        //收款约定时间
        saleContractCollectionPlan.setAgreedCollectionTime(oaOtherContractCollectionPlanPage.getAgreed_collection_time());
        //批次
        saleContractCollectionPlan.setBatch(oaOtherContractCollectionPlanPage.getBatch());
        return saleContractCollectionPlan;
    }

    /**
     * 提取OA其他合同详情数据
     */
    public static OtherContractDetail collectOaOtherContractDetailDate(OaOtherContractDetailPage oaOtherContractDetailPage){
        OtherContractDetail otherContractDetail=new OtherContractDetail();

        //分项介绍
        otherContractDetail.setItemizeIntroduce(oaOtherContractDetailPage.getItemize_introduce());
        //分项名称
        otherContractDetail.setItemizeName(oaOtherContractDetailPage.getItemize_name());
        //价税合计
        otherContractDetail.setTotalAmount(oaOtherContractDetailPage.getTotal_amount());
        //含税金额
        otherContractDetail.setTaxAmount(oaOtherContractDetailPage.getTax_amount());
        //税率
        otherContractDetail.setTaxRate(oaOtherContractDetailPage.getTax_rate());
        //折扣率
        otherContractDetail.setDiscountRate(oaOtherContractDetailPage.getDiscount_rate());
        //合同金额
        otherContractDetail.setContractAmount(oaOtherContractDetailPage.getContract_amount());
        //到货日期
        otherContractDetail.setPurchaseDate(oaOtherContractDetailPage.getPurchase_date());
        //备注
        otherContractDetail.setRemark(oaOtherContractDetailPage.getRemark());

        return otherContractDetail;
    }
}
